<?php
	// TODO Make this code better (MVC? Implement in Main MVC?)
	// TODO Hasmany functionality
	
	include_once('ajax.init.php');
	$id = $_POST['id'];

	// Load the items:
	// $item_info     = ma('SELECT * FROM `'.$dbprefix.'items` WHERE `id`='.$id);
	$result = $db->select('*', 'items', array('where'=>array('id'=>$id)));
	$item_info = mysql_fetch_assoc($result);
	
	// $category_info = ma('SELECT * FROM `'.$dbprefix.'categories` WHERE `id`='.$item_info['id_category'].';');
	$result = $db->select('*', 'categories', array('where'=>array('id'=>$item_info['id_category'])));
	$category_info = mysql_fetch_assoc($result); 
	$id_datatype   = $category_info['id_datatype'];
	
	$result = mq('SELECT A.* FROM `'.$dbprefix.'options` A, `'.$dbprefix.'datatype_option` B WHERE B.`id_datatype`='.$id_datatype.' AND B.`id_option` = A.`id`;');
	
	// Show the options:
	// Table style:
	
	echo '<table class="sub zebra">';
	/*
	echo '
	<tr>
		<th>Name</th>
		<th>Value</th>
	</tr>';
	*/
	while($option = ma($result)) {
		if(empty($option['description'])) { $option['description']=$option['name']; }
		$value = sr('SELECT `value` FROM `'.$dbprefix.'values` WHERE `id_item`='.$id.' AND `id_option`='.$option['id'].';');
		echo '<tr>';
		echo '<th>'.$option['description'].':</th>';
		if($option['type']=='category') {
			// Create a new expandable list:
			echo '<td class="expandable"><span class="expand"><span class="arrow">+</span><var class="id">'.$value.'</var></span>'.sr('SELECT `name` FROM `'.$dbprefix.'items` WHERE `id`='.$value.';').'</td>';
		} else {
			// Show value
			echo '<td>'.$value.'</td>';			
		}
		echo '</tr>';
	}
	echo '</table>';
	
	
	// Definition list style:
	/*
	echo '<dl>';
	while($option = ma($result)) {
		if(empty($option['description'])) { $option['description']=$option['name']; }
		$value = sr('SELECT `value` FROM `'.$dbprefix.'values` WHERE `id_item`='.$id.' AND `id_option`='.$option['id'].';');
		echo '<dt>'.$option['description'].'</dt>';
		if($option['type']=='category') {
			// Create a new expandable list:
			echo '<dd class="expandable"><span class="expand"><span class="arrow">+</span><var class="id">'.$value.'</var></span>'.sr('SELECT `name` FROM `'.$dbprefix.'items` WHERE `id`='.$value.';').'</dd>';
		} else {
			// Show value
			echo '<dd>'.$value.'</dd>';			
		}
	}
	echo '</dl>';	
	*/
?>